package com.order.controller.seller;


import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.order.dto.OrderDTO;
import com.order.pojo.OrderMaster;
import com.order.pojo.ProductCategory;
import com.order.service.IOrderMasterService;
import com.order.service.impl.PushMessageServiceImpl;
import com.order.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单主信息接口")
@RestController
@RequestMapping("/seller/order-master")
public class OrderMasterController {

    @Autowired
    IOrderMasterService orderMasterService;

    @Autowired
    PushMessageServiceImpl pushMessageService;

    @ApiOperation(value = "获取所有订单信息")
    @GetMapping("/")
    public Result getAllOrderMaster() {
        List<OrderMaster> orderMasters = orderMasterService.list();
        if (orderMasters == null || orderMasters.isEmpty()) {
            return Result.error("没有订单信息");
        }
        return Result.success(orderMasters);
    }

    @ApiOperation(value = "根据ID获取订单信息")
    @GetMapping("{orderId}")
    public Result getOrderMasterById(@PathVariable("orderId") String orderId) {
        OrderMaster orderMaster = orderMasterService.getById(orderId);
        if (orderMaster == null) {
            return Result.error("订单信息不存在");
        }
        return Result.success(orderMaster);
    }

    @ApiOperation(value = "添加订单信息")
    @PostMapping("/add")
    public Result addOrderMaster(@RequestBody OrderMaster orderMaster) {
        if (orderMasterService.save(orderMaster)) {
            return Result.success("订单信息添加成功");
        } else {
            return Result.error("订单信息添加失败");
        }
    }

    @ApiOperation(value = "更新订单信息")
    @PutMapping("/update")
    public Result updateOrderMaster(@RequestBody OrderMaster orderMaster) {
        orderMaster.setUpdateTime(DateUtil.now());
        if (orderMasterService.updateById(orderMaster)) {
            return Result.success("订单信息更新成功");
        } else {
            return Result.error("订单信息更新失败");
        }
    }

    @ApiOperation(value = "删除订单信息")
    @DeleteMapping("/delete/{orderId}")
    public Result deleteOrderMaster(@PathVariable("orderId") String orderId) {
        OrderMaster orderMaster = new OrderMaster(orderId, 1);
        if (orderMasterService.updateById(orderMaster)) {
            return Result.success("订单信息删除成功");
        } else {
            return Result.error("订单信息删除失败");
        }
    }

    @ApiOperation(value = "根据页面获取所有订单信息")
    @GetMapping("/selectPage")
    public Result getOrderMasterByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "8") Integer size,
                                       OrderMaster orderMaster) {
        PageInfo<OrderMaster> pageInfo = orderMasterService.getOrderMasterByPage(page, size, orderMaster);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "取消订单信息")
    @PostMapping("/cancel/")
    public Result cancelOrderMaster(@RequestBody OrderMaster orderMaster) {
//        orderMaster.setBuyerOpenid("wechatOpenId"); // 模拟一个openid
        try{
            if (orderMasterService.cancelOrder(orderMaster.getOrderId(), orderMaster.getBuyerOpenid())) {
                //消息推送
                List<OrderDTO> orderDTOList = orderMasterService.getOrderList(orderMaster.getBuyerOpenid());
                for (OrderDTO orderDTO : orderDTOList) {
                    if(orderDTO.getOrderId().equals(orderMaster.getOrderId())){
                        //消息推送
                        pushMessageService.orderStatus(orderDTO.getOrderId());
                    }
                }

                return Result.success("订单取消成功");
            } else {
                return Result.error("订单取消失败");
            }
        }catch (Exception e){
            return Result.error("订单取消失败");
        }
    }
}
